<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>add cate</title>
	<link rel="stylesheet" href="styles/backstage.css">
	<link href="./styles/global.css"  rel="stylesheet"  type="text/css" media="all" />

	<script type="text/javascript" src="./scripts/jquery-1.6.4.js"></script>
	<script type="text/javascript">
		$(document).ready(function(){
        	$("#selectFileBtn").click(function(){

        		var a = document.getElementsByName('bPic');
        		if(a.length > 0){
        			alert("只可以添加一张图片");
        			return false;
        		}

        		$fileField = $('<input type="file" name="thumbs[]"/>');
        		$fileField.hide();
        		$("#attachList").append($fileField);
        		$fileField.trigger("click");
        		$fileField.change(function(){
	        		$path = $(this).val();
	        		$filename = $path.substring($path.lastIndexOf("\\")+1);
	        		$attachItem = $('<div name="bPic" class="attachItem"><div class="left">a.gif</div><div class="right"><a href="#" title="删除附件">删除</a></div></div>');
	        		$attachItem.find(".left").html($filename);
	        		$("#attachList").append($attachItem);		
        		});
        	});
        	$("#attachList>.attachItem").find('a').live('click',function(obj,i){
        		$(this).parents('.attachItem').prev('input').remove();
        		$(this).parents('.attachItem').remove();
        	});


        	$("#mPicBtn").click(function(){

        		var a = document.getElementsByName('mPic');
        		if(a.length > 0){
        			alert("只可以添加一张图片");
        			return false;
        		}

        		$fileField = $('<input type="file" name="thumbs[]"/>');
        		$fileField.hide();
        		$("#mPicList").append($fileField);
        		$fileField.trigger("click");
        		$fileField.change(function(){
	        		$path = $(this).val();
	        		$filename = $path.substring($path.lastIndexOf("\\")+1);
	        		$mPic = $('<div name="mPic" class="attachItem"><div class="left">a.gif</div><div class="right"><a href="#" title="删除附件">删除</a></div></div>');
	        		$mPic.find(".left").html($filename);
	        		$("#mPicList").append($mPic);		
        		});
        	});
        	$("#mPicList>.attachItem").find('a').live('click',function(obj,i){
        		$(this).parents('.attachItem').prev('input').remove();
        		$(this).parents('.attachItem').remove();
        	});
        });


		function check(){

			if(document.getElementById('cName').value == ''){
				alert("请输入分类名称!");
				return false;
			}
			if(document.getElementsByName('bPic').length < 1 ){
        		alert("请添加背景图像");
        		return false;
        	}

        	if(document.getElementsByName('mPic').length < 1 ){
        		alert("请添加主图像");
        		return false;
        	}
			return checkCate();

		}

		function checkCate(){
			var url = "doAdminAction.php?act=checkCate";  
			var name = document.getElementById('cName').value
			var flag = true;
			$.ajax({  
				type: "POST",  
				url: url,  
				async: false,
			// dataType: "json",  
			data: {cateName:name},  
			success: function(msg){ 
				if(msg){
					alert(msg); 
					flag = false;
				}
			}
		});
			return flag;
		}

	</script>

</head>
<body>
	<br/>
	<h3>添加分类</h3>
	<br/>
	<form action="doAdminAction.php?act=addCate" method="post" enctype="multipart/form-data" onsubmit="return check()">
		<table class="table" cellpadding="5" cellspacing="0" >
			<tr>
				<th align="right">分类名称</th>	
				<td><input type="text" id = "cName" name="cName" class="search" placeholder="请输入分类名称"/></td>
			</tr>

			<tr>
				<th align="right">背景图像</th>
				<td>
					<a href="javascript:void(0)" id="selectFileBtn" class="btn">添加图片</a>
					
					<div id="attachList" class="attachList" class="clear"></div>
				</td>
			</tr>
			<tr>
				<th align="right">主图像</th>
				<td>
					<a href="javascript:void(0)" id="mPicBtn" class="btn">添加图片</a>
					
					<div id="mPicList" class="attachList" class="clear"></div>
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center"><input type="submit" class="btn"  value="添加分类"/></td>
			</tr>

		</table>
	</form>
</body>
</html>